package com.example.layui_project.controller;

import com.example.layui_project.comm.action.BaseController;
import com.example.layui_project.dto.JsonData;
import com.example.layui_project.dto.TableParams;
import com.example.layui_project.dto.TableResult;
import com.example.layui_project.entity.SysUser;
import com.example.layui_project.enums.ResultCode;
import com.example.layui_project.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @GetMapping("toUserList.do")
    public String toUserList(){
        boolean permission = super.checkPermission();
        if (!permission){
            // 说明权限校验未通过返回
            return "error";
        }
        return "user/userList";
    }
    @GetMapping("toAddUserList.do")
    public String toAddUserList(){
        return "user/add";
    }
    @RequestMapping("findData.do")
    @ResponseBody
    public TableResult findData(int page, int limit,String username){
        TableParams tableParams = new TableParams();
        HashMap<String, Object> ps = new HashMap<>();
        ps.put("username",username);
        tableParams.setPs(ps);
        tableParams.setPageNum(page-1);
        tableParams.setPageSize(limit);
        TableResult<SysUser> sysUserTableResult = userService.find(tableParams);
        sysUserTableResult.setCode(0);
        return sysUserTableResult;
    }

    @PostMapping("add.do")
    @ResponseBody
    public JsonData add(@RequestBody SysUser user){
        try {
            boolean add = userService.add(user);
            if(add){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("del.do")
    @ResponseBody
    public JsonData del(Integer id){
        try {
            boolean del = userService.del(id);
            if(del){
                return JsonData.success();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }

    @PostMapping("update.do")
    @ResponseBody
    public JsonData update(@RequestBody SysUser user){
        try {
            boolean update = userService.update(user);
            if(update){
                return JsonData.success(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getInfo());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonData.fail(ResultCode.SYSTEM_BUSY.getCode(),ResultCode.SYSTEM_BUSY.getInfo());
    }
}
